package com.groupfifteen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.groupfifteen.common.R;
import com.groupfifteen.entity.SysStudentScoreEntity;

import com.groupfifteen.service.SysStudentScoreService;
import com.groupfifteen.util.ApiResultHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("sysstudent/score")
public class SysStudentScoreController {

    @Autowired
    private SysStudentScoreService sysStudentScoreService;

    @PostMapping("/query")
    public IPage<SysStudentScoreEntity> queryStudentScores(@RequestBody SysStudentScoreEntity sysStudentScoreEntity,
                                                           @RequestParam(defaultValue = "1") int pageNo,
                                                           @RequestParam(defaultValue = "10") int pageSize) {
        return sysStudentScoreService.QueryStudentScores(sysStudentScoreEntity, pageNo, pageSize);
    }
    @PostMapping("addstudentscore")
    public String AddStudentScore(@RequestBody SysStudentScoreEntity sysStudentScoreEntity) {
        Integer result = sysStudentScoreService.AddStudentScore(sysStudentScoreEntity);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }
    //
    @DeleteMapping("/deletestudentscore/{ScoreId}")
    public String DeleteStudentScore(@PathVariable("ScoreId") Integer ScoreId) {
        Integer result = sysStudentScoreService.DeleteStudentScore(ScoreId);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }

    @GetMapping("/getstudentscore/{ScoreId}")
    public SysStudentScoreEntity GetStudentScore(@PathVariable("ScoreId") Integer ScoreId) {
        SysStudentScoreEntity result = sysStudentScoreService.GetStudentScore(ScoreId);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }

    @PutMapping("/updatestudentscore/{ScoreId}")
    public String UpdateStudentScore(@PathVariable("ScoreId") int  ScoreId,@RequestBody SysStudentScoreEntity sysStudentScoreEntity) {
        sysStudentScoreEntity.setScoreId(ScoreId);
        Integer result = sysStudentScoreService.UpdateStudentScore(sysStudentScoreEntity);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }
    /**
     * 查询所有考试成绩
     *
     * @return 考试成绩
     */
    @GetMapping("/scores")
    public R<List<SysStudentScoreEntity>> findAll() {
        List<SysStudentScoreEntity> res = sysStudentScoreService.findAll();
        return ApiResultHandler.buildApiResult(200, "查询所有学生成绩", res);
    }

    /**
     * 查询考试成绩 分页
     *
     * @param page      分页页数
     * @param size      分页大学
     * @param studentId 学生id
     * @return 考试成绩
     */
    @GetMapping("/score/{page}/{size}/{studentId}")
    public R<Page<SysStudentScoreEntity>> findById(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            @PathVariable("studentId") Integer studentId
    ) {
        IPage<SysStudentScoreEntity> res = sysStudentScoreService.findById(new Page<>(page, size), studentId);
        return ApiResultHandler.buildApiResult(200, "根据ID查询成绩", res);
    }

    /**
     * 查询考试成绩 不分页
     *
     * @param studentId 学生id
     * @return 考试成绩
     */
    @GetMapping("/score/{studentId}")
    public R<List<SysStudentScoreEntity>> findById(@PathVariable("studentId") Integer studentId) {
        List<SysStudentScoreEntity> res = sysStudentScoreService.findById(studentId);
        if (!res.isEmpty()) {
            return ApiResultHandler.buildApiResult(200, "根据ID查询成绩", res);
        } else {
            return ApiResultHandler.buildApiResult(400, "ID不存在", res);
        }
    }

    /**
     * 添加学生成绩
     *
     * @param score 学生成绩
     * @return 考试成绩
     */
    @PostMapping("/score")
    public R add(@RequestBody SysStudentScoreEntity score) {
        int res = sysStudentScoreService.add(score);
        if (res == 0) {
            return ApiResultHandler.buildApiResult(400, "成绩添加失败", res);
        } else {
            return ApiResultHandler.buildApiResult(200, "成绩添加成功", res);
        }
    }

    /**
     * 通过考试编号 查询学生成绩
     *
     * @param examCode 考试编号
     * @return 考试成绩
     */
    @GetMapping("/scores/{examCode}")
    public R<List<SysStudentScoreEntity>> findByExamCode(@PathVariable("examCode") Integer examCode) {
        List<SysStudentScoreEntity> scores = sysStudentScoreService.findByExamCode(examCode);
        return ApiResultHandler.buildApiResult(200, "查询成功", scores);
    }
}